from django import forms
from skran.models import Project, History
import datetime


class TaskForm(forms.ModelForm):        
    
    #Save task using form values
    def save(self, user, id_project=None, commit=True):      
        task = super(TaskForm, self).save(commit=False)  
        task.project = Project.objects.get(pk=id_project)
        
        if commit:
            task.save()
        return task
    
class HistoryForm(forms.ModelForm):        
    check = forms.BooleanField(widget=forms.CheckboxInput(),required=False)
    task_formset = forms.formsets.BaseFormSet() 
    
    class Meta:
        model = History
        fields = ('name', 'description','estimate','tags')
    
    def __init__(self, *args, **kwargs):
        self.base_fields['description'].widget = forms.TextInput()
        
        super(HistoryForm, self).__init__(*args, **kwargs)        
            
    def delete(self):
        history = self.instance
        history.delete()
                          
    #Save task using form values
    def save(self, user, project,sprint,commit=True):      
        history = super(HistoryForm, self).save(commit=False)

        history.created_by = user.username
        history.updated_by = user.username
        history.create_date = datetime.datetime.now()
        history.last_update = datetime.datetime.now()
        
        history.sprint = sprint
        history.project = project
        
        
        if commit:
            history.save()
        return history